/********************************************************************************
* File Name:	input.cpp
* Description:	第14章实例训练
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include "input.h"
#include "ui_input.h"
#include <QtSql>
#include <QMessageBox>
#include "database.h"

Input::Input(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Input)
{
    ui->setupUi(this);
}

Input::~Input()
{
    delete ui;
}

void Input::on_btnOk_clicked()
{
    QString tempscore=QString::number(this->score,10);
    QString tempname=ui->leName->text();
    QString id;
    if(!database::Open())
    {
        QMessageBox::about(this,"Message","connect error");
        return;
    }
    QSqlQuery query;
    query.exec("select max(ID) from list");
    if(!query.next())
        return;
    else
        id = QString::number(query.value(0).toInt()+1);

    if(sum<5)
        query.exec("insert into list values('"+id+"','"+tempname+"','"+tempscore+"')");
    else if(sum>=5)
    {
        QString tempid;
        query.exec("select max(ID) from list group by Score having Score<=all(select min(Score) from list)");
        if(query.next())
            tempid=query.value(0).toString();
        query.exec("delete from list where ID="+tempid);
        query.exec("insert into list values('"+id+"','"+tempname+"','"+tempscore+"')");
    }
    database::Close();
    this->close();
}

void Input::on_btnQuit_clicked()
{
    this->close();
}
